﻿@page "/"
@using Blazored.LocalStorage
@using Model.system.login;
@using daonaite.ui.Api
@using daonaite.ui.Util
@using Microsoft.AspNetCore.SignalR.Client
@layout LoginLayout
@inject HttpHelp http
@inject apiLogin loginurl
@inject RulesHelp rule
@inject IMessageService message
@inject ILocalStorageService localStorage
@inject NavigationManager NavigationManager

<div style="height:100vh;
background:url('images/loginbj1.webp'); background-repeat: no-repeat; background-position: center; background-size: cover;">
    <Card Title="刀奈特通用管理系统" Style="position:absolute; left:30%;top:30%; width:450px;  background-color: rgba(255, 255, 255, 0.9);">
        <Form Model="@form" LabelColSpan="5" WrapperColSpan="19" OnFinish="OnFinish" ValidateMode="@FormValidateMode.Rules">
            <FormItem Label="用户名" Rules="@rule.required("请输入用户名")">
                <Input @bind-Value="@context.username"></Input>
            </FormItem>
            <FormItem Label="密码" Rules="@rule.required("请输入密码")">
                <InputPassword @bind-Value="@context.password"></InputPassword>
            </FormItem>
            <FormItem Label="验证码" Rules="@rule.required("请输入验证码")">
                <Row>
                    <Col Span="16">
                    <Input @bind-Value="@context.yanzhengma"></Input>
                    </Col>
                    <Col Span="8">
                    <Image Src="@tupian" Preview="false" OnClick="getyanzhengmaAsync"></Image>
                    </Col>
                </Row>
            </FormItem>
            <FormItem WrapperColOffset="5">
                <Button Type="@ButtonType.Primary" HtmlType="submit">登录</Button>
            </FormItem>
        </Form>
    </Card>
</div>

@code {
    Dto_login form = new Dto_login();
    string tupian = "";

    protected override async Task OnInitializedAsync()
    {


        var _hubclient = new HubConnectionBuilder().WithUrl("http://127.0.0.1:5000/myhub").WithAutomaticReconnect().Build();
        _hubclient.On<string>("showMsg", (msg) =>
        {
            message.Success(msg);
        });
        await _hubclient.StartAsync();


        await base.OnInitializedAsync();
        await getyanzhengmaAsync();

        var tk = await localStorage.GetItemAsStringAsync("token");
        if (!string.IsNullOrEmpty(tk))
        {
            NavigationManager.NavigateTo("/home");
        }
    }

    private async Task getyanzhengmaAsync()
    {
        var result = await http.getAsync<string>(loginurl.getImage);
        if (result.Code == 200)
        {
            form.code = result.Data;
            tupian = $"data:image/jpg;base64,{result.Message}";
        }
    }

    private async void OnFinish(EditContext editContext)
    {
        var result = await http.posttAsync<string, Dto_login>(loginurl.denglu, form);
        if (result.Code == 200)
        {
            await localStorage.SetItemAsStringAsync("token", result.Data);
            message.Success("登录成功");
            NavigationManager.NavigateTo("/home");
        }
    }
}
